package arsmodeller.properties;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.views.properties.ColorPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.eclipse.ui.views.properties.TextPropertyDescriptor;

import arsmodeller.model.BusinessProcess;
import arsmodeller.model.Employee;
import arsmodeller.model.MainModel;
import arsmodeller.model.Node;
import arsmodeller.model.Service;

public class NodePropertySource implements IPropertySource {
	private Node node;

	public NodePropertySource(Node node) {
		this.node = node;
	}

	@Override
	public Object getEditableValue() {
		return null;
	}

	@Override
	public IPropertyDescriptor[] getPropertyDescriptors() {
		ArrayList<IPropertyDescriptor> properties = new ArrayList<IPropertyDescriptor>();
		if (node instanceof MainModel)

			properties
					.add(new PropertyDescriptor(Node.PROPERTY_RENAME, "Name"));

		properties
				.add(new PropertyDescriptor(MainModel.PROPERTY_OWNER, "Owner"));
		if (node instanceof Service) {
			properties
					.add(new PropertyDescriptor(Node.PROPERTY_RENAME, "Name"));
			properties
					.add(new PropertyDescriptor(Service.PROPERTY_WSLD, "WSDL"));
		}
		if (node instanceof Employee) {
			properties
					.add(new PropertyDescriptor(Node.PROPERTY_RENAME, "Name"));
			properties.add(new PropertyDescriptor(Employee.PROPERTY_FIRSTNAME,
					"FirstName"));
		}
		if (node instanceof BusinessProcess) {
			properties
					.add(new PropertyDescriptor(Node.PROPERTY_RENAME, "Name"));
			properties.add(new PropertyDescriptor(BusinessProcess.JAVA_CODE,
					"JavaCode"));
		}
		return properties.toArray(new IPropertyDescriptor[0]);
	}

	@Override
	public Object getPropertyValue(Object id) {
		JOptionPane.showMessageDialog(null, id.toString());
		if (id.equals(Node.PROPERTY_RENAME))
			return node.getName();
		if (id.equals(Service.PROPERTY_COLOR))
			return ((Service) node).getColor().getRGB();
		if (id.equals(Employee.PROPERTY_FIRSTNAME))
			return ((Employee) node).getPrenom();
		if (id.equals(MainModel.PROPERTY_OWNER))
			return ((MainModel) node).getOwner();
		if (id.equals(BusinessProcess.JAVA_CODE))
			return ((BusinessProcess) node).getJavaCode();
		return null;
	}

	@Override
	public boolean isPropertySet(Object arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void resetPropertyValue(Object arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setPropertyValue(Object id, Object value) {
		if (id.equals(Node.PROPERTY_RENAME))
			node.setName((String) value);
		else if (id.equals(Service.PROPERTY_COLOR)) {
			Color newColor = new Color(null, (RGB) value);
			((Service) node).setColor(newColor);
		} else if (id.equals(Service.PROPERTY_WSLD)) {
			try {
				String newWSDL = (String) value;
				((Service) node).setPathToWSDL(newWSDL);
			} catch (NumberFormatException e) {
			}
		} else if (id.equals(MainModel.PROPERTY_OWNER)) {
			try {
				String name = String.valueOf(value);
				((MainModel) node).setOwner(name);
			} catch (NumberFormatException e) {
			}
		}

	}
}
